<template>
  <view>
    <view>
        <my-search></my-search>
    </view>
    <view class="outBox">

      <scroll-view scroll-y="true" class="left-scr" :style="{height: wh+'px'}">
        <view v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i==active?'active':'']" @click="switchLeftTabHandler(i)">
            {{item.cat_name}}
          </view>
        </view>
      </scroll-view>
      <scroll-view scroll-y="true" class="right-scr" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <view v-for="(item2,i2) in secondList" :key="i2">
          <!-- 二级菜单标题 -->
          <view class="second_cate_item">
            {{item2.cat_name}}
          </view>
          <!-- 三级菜单 图片和名称 -->
          <view class="third_cate_box">
            <view v-for="(item3,i3) in item2.children" :key="i3" class="third_cate_item"
              @click="switchToGoods(item3.cat_id)">
              <image :src="item3.cat_icon" class="third_cate_item_img"></image>
              {{item3.cat_name}}
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        wh: 0,
        cateList: [],
        active: 0,
        secondList: [],
        scrollTop: 1
      };
    },
    onLoad() {
      this.wh = uni.getSystemInfoSync().windowHeight
       this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.cateList = res.message
        this.secondList = this.cateList[0].children
        console.log(this.cateList)
      },
      switchLeftTabHandler(i) {
        this.active = i
        this.secondList = this.cateList[i].children
        this.scrollTop = this.scrollTop ? 0 : 1
      },
      switchToGoods(id) {
        uni.navigateTo({
          url: "/packageA/goods_detail/goods_detail?cid=" + id
        })
      }
    }
  }
</script>

<style lang="scss">
  .third_cate_box {
    display: flex;
    flex-wrap: wrap;

    .third_cate_item {
      align-items: center;
      width: 33.33%;
      display: flex;
      flex-direction: column;
      font-size: 12px;

      .third_cate_item_img {
        width: 60px;
        height: 60px
      }
    }
  }



  .second_cate_item {
    font-size: 20px;
    font-weight: bold;
    text-align: center;
  }

  .left-scr {
    width: 120px;
  }

  .left-scroll-view-item {
    background-color: #f7f7f7;
    line-height: 60px;
    text-align: center;
    font-size: 12px;

    &.active {
      background-color: aqua;
      position: relative;

      &::before {
        content: " ";
        display: block;
        width: 3px;
        height: 30px;
        background-color: red;
        position: absolute;
        top: 25%;
      }
    }
  }

  .outBox {
    display: flex;
  }
</style>